﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Configuration;

/// <summary>
/// 包含存取AppBaseDatabase数据库的Access工具类和所有Entity实体类。
/// <br/>这些类均实现为Partial方式，使用时可以在单独的文件中扩展这些类的成员。
/// </summary>
namespace Ruandao.HostServer.SqlEntityAccess
{
    using Ruandao.MiniAccess;
	using Ruandao.MiniAccess.SqlServer;
    using Ruandao.Foundation;
    using System.Runtime.Caching;

    /// <summary>
	/// 该类封装了SqlServerAccess针对具体某个数据库的IAccess的一个实例，提供了存取AppBaseDatabase数据库的工具方法。
    /// <br/>该类使用了单例模式，没有公开构造方法，只能通过GetInstance()获取类的实例。
    /// </summary>
    public sealed partial class HostServerAccess : SqlServerAccess
    {
        /// <summary>`
        /// 
        /// </summary>
        /// <returns></returns>
        public static HostServerAccess GetInstance()
        {
            ObjectCache cache = MemoryCache.Default;
            string connString = GetConnectionString();
            string instanceChaeKey = "HOSTSERVERACCESS_INSTANCES_" + connString;
            HostServerAccess instance = cache[instanceChaeKey] as HostServerAccess;

            if (instance == null)
            {
                instance = new HostServerAccess(connString);
                CacheItemPolicy policy = new CacheItemPolicy();
                cache.Set(instanceChaeKey, instance, policy);
            }
            return instance;
        }

        private static string GetConnectionString(string providerName)
        {
            string dbconnection = ConfigurationManager.AppSettings["HosterServerConnectionName"];
            if (!string.IsNullOrEmpty(dbconnection))
            {
                return ConfigurationManager.ConnectionStrings[dbconnection].ConnectionString;
            }
            return SiteWare.Current.Site.ConnectionString;
        }
    }
}
